﻿; (function (global) {

    'use strict';

    global.Solver = function Solver(stategies) {
        this.stategies = stategies;
    }

    Solver.prototype.solve = function (grid) {
        var performedResults = [];
        var performedResult = null;
        var emptyLength = GridHelper.getCellsOfValue(grid, 0).length;
        SolvingHelper.fillCandidates(grid);
        do {
            performedResult = this.stepSolve(grid);
            if (performedResult) {
                performedResults.push(performedResult);
                if (performedResult.strategy.type == 'filler') {
                    emptyLength--;
                    if (emptyLength === 0) {
                        break;
                    }
                }
            }
        }
        while (performedResult);

        return { performedResults: performedResults, solved: emptyLength === 0 };
    }

    Solver.prototype.stepSolve = function (grid) {

        for (var i = 0, len = this.stategies.length; i < len; i++) {
            var strategy = this.stategies[i];
            var result = strategy.perform(grid);
            if (result) {
                //console.log('start', strategy)
                return { result: result, strategy: strategy };
            }

        }

        return null;
    }


}(this));
